﻿using System;
using System.IO;
using System.Numerics;
using GameProtocol.Record;
using Google.Protobuf;
using Sirenix.Utilities;
using TEngine;
using UnityEngine;

namespace GameLogic.Data
{
    public partial class GameData : BehaviourSingleton<GameData>
    {
        private long _version;
        private Record m_Record;
        public string m_SaveDir = "./save/";
        public string m_SaveFileName = "player.sav";

        public GameData()
        {
#if UNITY_ANDROID
        m_SaveDir = Application.persistentDataPath + "save/";
#endif
        }
        public override void Awake()
        {
            base.Start();
        }

        public override void Destroy()
        {
            base.Destroy();
        }


        public Record Record
        {
            get { return m_Record; }
        }
          
        public void Load()
        {
            byte[] data = UtilsUnity.LoadFile(m_SaveDir+m_SaveFileName);
            if (data == null)
                m_Record = new Record();
            else
                m_Record = Record.Parser.ParseFrom(data);
        }
        public void Save()
        {
            var bytes = m_Record.ToByteArray();
            var fullPath = m_SaveDir + m_SaveFileName;

            if(!Directory.Exists(m_SaveDir))
                Directory.CreateDirectory(m_SaveDir);
            UtilsUnity.SaveFile(bytes, fullPath);
        }
    }
}